package com.heer.imagepick;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;

public class PhotoWallMultipleAdapter extends BaseAdapter {
	private Activity context;
	private ArrayList<String> imagePathList = null;
	private Handler mHandler;
	private SDCardImageLoader loader;
	// 记录是否被选择
	private SparseBooleanArray selectionMap;
	// 选择条数
	private int count;

	public void setCount(int count) {
		this.count = count;
	}

	private String SD_CARD_TEMP_DIR;

	public String getSD_CARD_TEMP_DIR() {
		return SD_CARD_TEMP_DIR;
	}

	public void setSD_CARD_TEMP_DIR(String sD_CARD_TEMP_DIR) {
		SD_CARD_TEMP_DIR = sD_CARD_TEMP_DIR;
	}

	public PhotoWallMultipleAdapter(Activity context,
									ArrayList<String> imagePathList, Handler mHandler) {
		this.context = context;
		this.imagePathList = imagePathList;

		loader = new SDCardImageLoader(DensityUtils.getScreenW(context),
				DensityUtils.getScreenH(context));
		selectionMap = new SparseBooleanArray();
		this.mHandler = mHandler;
	}

	@Override
	public int getCount() {
		return imagePathList == null ? 0 : imagePathList.size();
	}

	@Override
	public Object getItem(int position) {
		return imagePathList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		String filePath = (String) getItem(position);

		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.photo_wall_item, null);
			holder = new ViewHolder();

			holder.imageView = (ImageView) convertView
					.findViewById(R.id.photo_wall_item_photo);
			holder.checkBox = (CheckBox) convertView
					.findViewById(R.id.photo_wall_item_cb);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if (filePath.equals("index")) {
			holder.imageView.setImageResource(R.drawable.icon41);
			holder.checkBox.setVisibility(View.GONE);
			holder.imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					String state = Environment.getExternalStorageState();
					if (state.equals(Environment.MEDIA_MOUNTED)) {
						SD_CARD_TEMP_DIR = Environment
								.getExternalStorageDirectory()
								+ File.separator
								+ "tmpPhoto.jpg";
						Intent takePictureFromCameraIntent = new Intent(
								MediaStore.ACTION_IMAGE_CAPTURE);
						takePictureFromCameraIntent.putExtra(
								MediaStore.EXTRA_OUTPUT,
								Uri.fromFile(new File(SD_CARD_TEMP_DIR)));
						context.startActivityForResult(
								takePictureFromCameraIntent, 2);
					} else {
						Toast.makeText(context,
								context.getString(R.string.sdcard_empty),
								Toast.LENGTH_LONG).show();
					}
				}
			});
		} else {
			// tag的key必须使用id的方式定义以保证唯一，否则会出现IllegalArgumentException.
			holder.checkBox.setTag(R.id.tag_first, position);
			holder.checkBox.setVisibility(View.VISIBLE);
			holder.checkBox.setTag(R.id.tag_second, holder.imageView);
			holder.checkBox
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
						@Override
						public void onCheckedChanged(CompoundButton buttonView,
								boolean isChecked) {
							Integer position = (Integer) buttonView
									.getTag(R.id.tag_first);
							ImageView image = (ImageView) buttonView
									.getTag(R.id.tag_second);
							selectionMap.put(position, isChecked);
							if (isChecked) {
								image.setColorFilter(context.getResources()
										.getColor(R.color.image_checked_bg));

							} else {
								image.setColorFilter(null);
							}
						}
					});
			holder.checkBox.setChecked(selectionMap.get(position));
			holder.imageView.setTag(filePath);
			holder.imageView.setTag(R.id.tag_first, position);
			holder.imageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View buttonView) {
					// TODO Auto-generated method stub
					Integer position = (Integer) buttonView
							.getTag(R.id.tag_first);
					// selectionMap.put(position, holder.checkBox.isChecked());
					if (selectionMap.get(position)) {
						holder.checkBox.setChecked(false);
						count--;
					} else {
						holder.checkBox.setChecked(true);
						count++;
					}
					if (count >= 10) {
						holder.checkBox.setChecked(false);
						count--;
						Message message = Message.obtain(mHandler, 0);
						message.sendToTarget();
					}
				}
			});
			loader.loadImage(4, filePath, holder.imageView);
		}

		return convertView;
	}

	private class ViewHolder {
		ImageView imageView;
		CheckBox checkBox;
	}

	public SparseBooleanArray getSelectionMap() {
		return selectionMap;
	}

	public void clearSelectionMap() {
		selectionMap.clear();
	}
}
